# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

library(plyr)
library(tidyverse)
library(lubridate)
library(magrittr)
library(broom)
library(scales)

all_panels_long <- read_rds("data/all_panels_long.rds")

t1 <- all_panels_long |> 
  filter(fc == 1)


# observations and date ranges

t2 <- t1 %>% 
  group_by(
    panel
  ) %>% 
  summarize(
    obs = n(),
    date_lo = admin_StartDate %>% 
      as.Date %>% 
      min,
    date_hi = admin_StartDate %>% 
      as.Date %>% 
      max
    ) %>% 
  modify_at(
    3:4,
    ~str_c(
      .x %>% mday %>% ordinal,
      " ",
      .x %>% month(label = T, abbr = T)
    )
  ) %>%
  mutate(
    obs = obs %>% prettyNum(big.mark = ",")
  ) %>% 
  pivot_longer(
   cols = -panel,
   values_to =   "vals",
   names_to = "cols" 
  ) %>% 
  pivot_wider(
    values_from = vals, 
    names_from = panel
    ) %>% 
  slice(
    c(2:3, 1)
  )
  
t3 <- t1 %>% 
  ungroup %>% 
  select(
    panel, lucid_age, lucid_education, lucid_race, lucid_female, lucid_hhi, lucid_pid_3, lucid_region
  ) %>% 
  mutate(
    lucid_age = lucid_age %>% 
      is_greater_than(100) %>% 
      ifelse(
        2020 - lucid_age,
        lucid_age
      ) %>% 
      round,
    lucid_age = lucid_age %>% 
      cut(
        c(-Inf, 34, 50, 65, Inf),
        labels = 
          c("18-34",
            "35-50",
            "51-65",
            ">65")
      ),
    lucid_education = lucid_education %>% 
      mapvalues(
        
        c("3rd Grade or less", "Middle School - Grades 4 - 8", "Completed some high school", 
          "High school graduate", "Other post high school vocational training", 
          
          "Completed some college, but no degree", "Associate's degree", 
          
          "Bachelor's degree",
          
          "Completed some graduate, but no degree", "Masters degree", "Doctorate degree"),
        c("HSD or less",
          "Some college",
          "BA degree",
          "Graduate education") %>% 
          rep(
            c(5, 2, 1, 3)
          ) 
      ) %>% 
      factor(
        c("HSD or less",
          "Some college",
          "BA degree",
          "Graduate education")
      ),
    lucid_race = lucid_race %>% 
      fct_infreq,
    lucid_female = lucid_female %>% 
      mapvalues(
        0:1,
        c("Male", "Female")
      ) %>% 
      fct_infreq,
    lucid_hhi = lucid_hhi %>% 
      mapvalues(
        t1$lucid_hhi %>% 
          factor %>% 
          levels,
        t1$lucid_hhi %>% 
          factor %>% 
          levels %>% 
          map_dbl(
            ~.x %>% 
              str_extract_all("\\d{1,3}\\,\\d{3}", simplify = T) %>% 
              str_remove_all("\\,") %>% 
              as.numeric %>% 
              mean
          )
      ) %>% 
      as.character %>% 
      as.numeric %>%
      cut(
        c(-Inf, 40000, 80000, 120000, Inf),
        c("<$40k", "$40-80k", "$80-120k", ">$120k")
      ) %>%
      factor(
        c("<$40k", "$40-80k", "$80-120k", ">$120k")
      ),
    lucid_pid_3 = lucid_pid_3 %>% 
      factor(
        c("Democrat",
          "Independent",
          "Republican")
      ),
    lucid_region = lucid_region %>% 
      fct_inorder()
  ) %>% 
  select(
    panel, lucid_female, lucid_race, lucid_age, everything()
  )


t4 <- t3 %>% 
  pivot_longer(
    -panel, 
    names_to = "nms",
    values_to = "vals"
  ) %>%  
  mutate(
    vals = vals %>% 
      factor(
        t3 %>% 
          select(-panel) %>% 
          map(
            ~levels(.x)
          ) %>% 
          unlist %>% 
          as.character
      ),
    nms = nms %>% 
      mapvalues(
        c("lucid_female", "lucid_race", "lucid_age", "lucid_education", 
          "lucid_hhi", "lucid_pid_3", "lucid_region"),
        c("Gender",
          "Race",
          "Age",
          "Education",
          "Income",
          "Partisanship",
          "Region")
        ) %>% 
      factor(
        c("Gender",
          "Race",
          "Age",
          "Education",
          "Income",
          "Partisanship",
          "Region")
        )
    ) %>% 
  group_by(panel, nms, vals) %>% 
  tally %>% 
  na.omit %>% 
  mutate(
    perc = n %>% 
      divide_by(
        n %>% sum
        ) %>% 
      multiply_by(100) %>%
      round %>% 
      as.character
    ) %>% 
  select(-n) %>% 
  pivot_wider(
    names_from = panel,
    values_from = perc
    )


t2 %>%
  rename(
    nms = cols
  ) %>% 
  bind_rows(
    t4
  ) %>% 
  select(
    nms, vals, everything()
  ) %>% 
  group_by(
    nms 
  ) %>% 
  mutate(
    nms2 = c(
      nms[[1]],
      "" %>% rep(times = n() %>% subtract(1))
    )
  ) %>% 
  ungroup %>% 
  mutate(
    nms = nms2
  ) %>% 
  select(-nms2) %>% 
  print(n = 29)

  